import pika
# main.py

from fastapi import FastAPI
from rabbitmq_service import RabbitMQService
import threading

app = FastAPI()


class AppInstance:
    def __init__(self):
        self.rabbitmq_service = RabbitMQService(
            rabbitmq_url='amqp://zhzt:123456@124.222.203.130:5672/',  # 根据实际情况修改
            exchange_name='action.fanout',
            queue_name='fanout.queue1'
        )

        threading.Thread(target=self.start_rabbitmq_consumer).start()

    def start_rabbitmq_consumer(self):
        self.rabbitmq_service.start_consuming(self.process_message)

    def process_message(self, body):
        # 在这里处理收到的消息
        print(f"Received message: {body}")

    def __call__(self, scope):
        if scope["type"] == "lifespan":
            return {"scope": scope}


app_instance = AppInstance()


@app.get("/")
def read_root():
    return {"message": "Hello World"}

